// pages/collectionReward/collectionReward.js
const app = getApp();
import {
  postRequest,
  getRequest,
  postParamsRequest
} from '../../api/request';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    submitLock: false,
    cupInfo: '',//杯信息
    isLoad: false,//是否加载
  },

  //返回上一页
  backPage() {
    wx.navigateBack({
      delta: 1
    })
  },

  //获取小程序集章卡活动详情
  getWxappCollectionCard(){
    let that = this;
    that.setData({
      isLoad: false,
      cupInfo: ''
    })
    wx.showLoading({
      title: '加载中...',
      mask: true,
    })
    getRequest("/api/activity/getWxappCollectionCard", {
      memberId: app.myInfo.memberId
    }).then((res) => {
      console.log(res.data.data);
      if (res.data.data != null) {
        that.setData({
          cupInfo: res.data.data,
        })
      }
    }).catch((err) => {
    }).finally(() => {
      // 停止下拉刷新效果
      wx.stopPullDownRefresh()
      wx.hideLoading();
      that.setData({
        isLoad: true,
      })
    });
  },
  //集章卡领取优惠券
  getCollectionReward(){
    console.log('卡卷优惠')
    let that = this;
    if(that.data.cupInfo.isGet == 0 || that.data.submitLock){
      return;
    }
    that.setData({
      submitLock: true,
    })
    wx.showLoading({
      title: '领取中...',
      mask: true,
    })
    getRequest("/api/activity/getCollectionReward", {
      memberId: app.myInfo.memberId,
      activityId: that.data.cupInfo.activityId
    }).then((res) => {   
      console.log(res.data.data);
      if (res.data.code == 0 && res.data.data) {
        this.setData({
          submitLock:false
        })
        wx.showToast({
          title: '领取成功',
        })
      }
    }).catch((err) => {
    }).finally(() => {
      wx.hideLoading();
      that.setData({
        submitLock: false,
      })
      that.getWxappCollectionCard()
    });
  },
  //集章卡兑换记录列表
  collectionRewardList(){
    let that = this;
    wx.navigateTo({
      url: '../collectionRewardList/collectionRewardList?num='+that.data.cupInfo.num+'&hadGet='+that.data.cupInfo.hadGet,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //如果没有token,则需要重新登录获取个人信息等，避免缺少id导致数据无法获取
    if (app.token == "" || app.token == undefined) {
      app.initLogin().then(res => {
        this.getWxappCollectionCard();
      }).catch(err => {})
    } else {
      this.getWxappCollectionCard();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getWxappCollectionCard();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})